var BMapGLLib = window.BMapGLLib = BMapGLLib || {};
!function() {
    var t = 0
      , i = BMapGLLib.TrackAnimation = function(t, i, e) {
        console.log('i._config : ', i);
        var o = i.getPath();
        if (i._config.linkRight) {
            for (var n = [], s = null, a = 0; a < o.length - 1; a++) {
                a || n.push(o[a]);
                var p = s || o[a];
                s = o[a + 1];
                Math.abs(s.lng - p.lng) > 180 && (s.lng < p.lng ? s.lng += 360 : s.lng -= 360),
                n.push(s)
            }
            o = n
        }
        this._map = t,
        this._polyline = i,
        this._totalPath = o,
        this._overallView = t.getViewport(o),
        this._status = 2,
        this._opts = {
            zoom: this._getZoom(),
            tilt: 55,
            heading: 0,
            duration: 1e4,
            delay: 0,
            overallView: !0
        },
        this._initOpts(e),
        this._expandPath = this._addPath(o),
        this._pauseTime = 0,
        this._last2Points = []
    }
    ;
    i.prototype._getZoom = function() {
        return Math.min(this._overallView.zoom + 1, this._map.getMaxZoom())
    }
    ,
    i.prototype._updateAniParams = function() {
        this._updatePathAni(),
        this._updateViewAni(),
        this._polyline.setPath(this._expandPath.slice(0, 1))
    }
    ,
    i.prototype._updatePathAni = function() {
        this._expandPath = this._addPath(this._totalPath)
    }
    ,
    i.prototype._updateViewAni = function() {
        this._overallView = this._map.getViewport(this._totalPath);
        for (var t = this._totalPath.length, i = [], e = this._opts.overallView ? this._opts.duration + 2e3 : this._opts.duration, o = 0; o < t; o++) {
            var n = this._totalPath[o]
              , s = this._pathPercents[o] * (this._opts.duration / e);
            i.push({
                center: new BMapGL.Point(n.lng,n.lat),
                zoom: this._opts.zoom,
                tilt: 0 === o ? 0 : this._opts.tilt,
                heading: 0 === o ? 0 : this._opts.heading,
                percentage: s
            })
        }
        this._opts.overallView && i.push({
            center: new BMapGL.Point(this._overallView.center.lng,this._overallView.center.lat),
            zoom: this._overallView.zoom - 1,
            tilt: 0,
            heading: 0,
            percentage: 1
        });
        var a = this
          , p = {
            duration: e,
            delay: 0,
            interation: 1
        };
        this._viewAni = new BMapGL.ViewAnimation(i,p),
        this._viewAni.addEventListener("animationend", function(t) {
            a._opts.onAnimateEnd && a._opts.onAnimateEnd(t)
        })
    }
    ,
    i.prototype._addPath = function(t) {
        for (var i = this._opts.duration / 10, e = t.length, o = 0, n = [], s = [], a = 1; a < e; a++) {
            var p = this._map.getDistance(t[a - 1], t[a]);
            n.push(p),
            o += p
        }
        for (var r = [0], a = 1; a < e; a++) {
            var h = (n[a - 1] / o).toFixed(2);
            r[a] = r[a - 1] + parseFloat(h, 10),
            s = s.concat(this._getPath(t[a - 1], t[a], h * i))
        }
        return this._pathPercents = r,
        s
    }
    ,
    i.prototype._getPath = function(t, i, e) {
        var o = [];
        if (0 === e)
            return o;
        for (var n = 0; n <= e; n++) {
            var s = new BMapGL.Point((i.lng - t.lng) / e * n + t.lng,(i.lat - t.lat) / e * n + t.lat);
            o.push(s)
        }
        return o
    }
    ,
    i.prototype._initOpts = function(t) {
        for (var i in t)
            t.hasOwnProperty(i) && (this._opts[i] = t[i])
    }
    ,
    i.prototype.start = function() {
        var t = this;
        setTimeout(function() {
            t._updateAniParams(),
            t._map.removeOverlay(t._polyline),
            t._map.addOverlay(t._polyline),
            t._status = 1,
            t._step(performance.now()),
            t._map.startViewAnimation(t._viewAni)
        }, this._opts.delay)
    }
    ,
    i.prototype.cancel = function() {
        this._clearRAF(),
        this._status = 2,
        t = 0,
        this._pauseTime = 0,
        this._map.cancelViewAnimation(this._viewAni),
        this._map.removeOverlay(this._polyline)
    }
    ,
    i.prototype.pause = function() {
        1 === this._status && (this._clearRAF(),
        this._map.pauseViewAnimation(this._viewAni),
        this._status = 3,
        this._isPausing = performance.now())
    }
    ,
    i.prototype.continue = function() {
        3 === this._status && (this._pauseTime += performance.now() - this._isPausing,
        this._isPausing = void 0,
        this._status = 1,
        this._step(performance.now()),
        this._map.continueViewAnimation(this._viewAni))
    }
    ,
    i.prototype._step = function(i) {
        if (2 === this._status)
            return void (t = 0);
        t || (t = i),
        i -= this._pauseTime;
        var e = (i - t) / this._opts.duration
          , o = Math.round(this._expandPath.length * e)
          , n = this._expandPath.slice(0, o);
        this._last2Points = n.slice(-4),
        this._polyline.setPath(n),
        i < t + this._opts.duration ? this._timer = window.requestAnimationFrame(this._step.bind(this)) : (t = 0,
        this._status = 2,
        this._pauseTime = 0)
    }
    ,
    i.prototype._clearRAF = function() {
        this._timer && window.cancelAnimationFrame(this._timer)
    }
    ,
    i.prototype.setZoom = function(t) {
        this._opts.zoom = t
    }
    ,
    i.prototype.getZoom = function(t) {
        return this._opts.zoom
    }
    ,
    i.prototype.setTilt = function(t) {
        this._opts.tilt = t
    }
    ,
    i.prototype.getTilt = function(t) {
        return this._opts.tilt
    }
    ,
    i.prototype.setDelay = function(t) {
        this._opts.delay = t
    }
    ,
    i.prototype.getDelay = function(t) {
        return this._opts.delay
    }
    ,
    i.prototype.setDuration = function(t) {
        this._opts.duration = t
    }
    ,
    i.prototype.setSpeed = function(i) {
        if (!(i <= 0) && this._viewAni) {
            var e = 3 === this._status && this._isPausing ? this._isPausing : performance.now()
              , o = this._opts.duration
              , n = o * (1 / i);
            t = e - (e - t - this._pauseTime) / o * n - this._pauseTime;
            var s = this._viewAni.animation
              , a = 3 === this._status && s._isPausing ? s._isPausing : performance.now()
              , p = s._options.duration
              , r = p * (1 / i)
              , h = s._beginTime;
            h = a - (a - h - s._pauseTime) / p * r - s._pauseTime,
            s.setBeginTime(h),
            s.setDuration(r),
            this.setDuration(n)
        }
    }
    ,
    i.prototype.getSpeed = function() {
        return this._opts._speedFactor || 1
    }
    ,
    i.prototype.getDuration = function(t) {
        return this._opts.duration
    }
    ,
    i.prototype.enableOverallView = function() {
        this._opts.overallView = !0
    }
    ,
    i.prototype.disableOverallView = function() {
        this._opts.overallView = !1
    }
    ,
    i.prototype.setPolyline = function(t) {
        this._polyline = t,
        this._totalPath = t.getPath()
    }
    ,
    i.prototype.getPolyline = function() {
        return this._polyline
    }
    ,
    i.prototype.getLastPoint = function() {
        return [this._last2Points[0], this._last2Points[3]]
    }
}();
